﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>体检信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_PersonMedicalBatcheId_FormItem" prop="Entity.PersonMedicalBatcheId">
								<template #label>
									<WtmLabelTooltip label="体检批次表" tooltip="体检批次表" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.PersonMedicalBatcheId" clearable filterable class="width100">
									<el-option v-for="item in stateData.AllSysPersonMedicalBatches" :key="item.Value" :value="item.Value" :label="item.Text"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Result_FormItem" prop="Entity.Result">
								<template #label>
									<WtmLabelTooltip label="体检结果" tooltip="体检结论" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Result" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_MedicalReportId_FormItem" prop="Entity.MedicalReportId">
								<template #label>
									<WtmLabelTooltip label="体检报告" tooltip="体检报告文件" />
								</template>
								<wtm-upload-file v-model="stateData.vmModel.Entity.MedicalReportId" clearable class="width100"></wtm-upload-file>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, nextTick } from 'vue';
import { SysPersonMedicalApi as api } from '/@/views/person/syspersonmedical/api';
import other from '/@/utils/other';
import { ISysPersonMedicalVm } from '/@/views/person/syspersonmedical/api/interface';
import { IViewModel } from '/@/types/common';

const ci = getCurrentInstance() as any;
const loading = ref(false);
// 标记PersonId是否应该禁用（编辑时通常应该禁用以保持数据一致性）
const shouldDisablePersonId = ref(true);
// 定义变量内容
const formRef = ref();
const stateData = reactive<IViewModel<ISysPersonMedicalVm>>({
	vmModel: {
		Entity: {
			ID: null,
			PersonId: null,
			PersonMedicalBatcheId: null,
			Result: null,
			MedicalReportId: null,
		},
	},

	AllSysPersons: [] as any[],
	AllSysPersonMedicalBatches: [] as any[],
});

// 取消
const onClose = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			api()
				.edit(stateData.vmModel)
				.then(() => {
					ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
					emit('refresh');
					closeDialog();
				})
				.catch((error) => {
					other.setFormError(ci, error);
				})
				.finally(() => {
					loading.value = false;
				});
		}
	});
};

// 页面加载时
onMounted(() => {
	loading.value = true;
	if (ci.attrs['wtmdata']) {
		stateData.vmModel.Entity.ID = ci.attrs['wtmdata'].ID;
	} 
	const promises: Promise<any>[] = [
		other.getSelectList('/api/SysPersonMedical/GetSysPersonMedicalBatches', [], false).then((x) => {
			stateData.AllSysPersonMedicalBatches = x;
		}),

		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((data: any) => other.setValue(stateData.vmModel, data)),
	];
	Promise.allSettled(promises).finally(() => {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	});
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog','updateHeaderButtons']);
// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: ci.proxy.$t('message._system.common.vm.submit'),
			type: 'primary',
			onClick: onSubmit,
			visible: true,
		},
		{
			text: ci.proxy.$t('message._system.common.vm.cancel'),
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];

	emit('updateHeaderButtons', headerButtons);
};
</script>
